package com.sundi.ecmarket.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.sundi.ecmarket.dto.EcCartDto;
import com.sundi.ecmarket.dto.ResultEntity;
import com.sundi.ecmarket.entity.EcCart;
import com.sundi.ecmarket.entity.EcGoods;
import com.sundi.ecmarket.service.IEcCartService;
import com.sundi.ecmarket.service.IEcGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 购物车商品表 前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-03-15
 */
@RestController
@RequestMapping("/ecCart")
@Slf4j
public class EcCartController {

    @Autowired
    IEcCartService cartService;

    @Autowired
    IEcGoodsService goodsService;


    @RequestMapping("/list")
    public ResultEntity list(Integer userId, HttpServletRequest request){
//        HttpSession session = request.getSession();
//        Object obj = session.getAttribute("user");
//        if (obj==null){
//            return ResultEntity.error("401","尚未登录",session.getId());
//        }
        Wrapper<EcCart> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id",userId);
        List<EcCart> ecCarts = cartService.selectList(wrapper);
        return ResultEntity.ok(ecCarts);
    }


    @RequestMapping("addCart")
    public ResultEntity addCart(@RequestBody EcCartDto ecCartDto, HttpServletRequest request){
//        HttpSession session = request.getSession();
//        Object obj = session.getAttribute("user");
//        if (obj==null){
//            return ResultEntity.error("401","尚未登录",session.getId());
//        }
        Wrapper<EcCart> wrapper = new EntityWrapper<>();
        wrapper.eq("goods_id",ecCartDto.getGoodsId());
        EcCart ecCart = cartService.selectOne(wrapper);
        if(ecCart!=null){
            return ResultEntity.ok("亲！您的购物车中已有此商品！！");
        }
        log.info("返回的数据为：{}",ecCartDto);
        Wrapper<EcGoods> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("id",ecCartDto.getGoodsId());
        EcGoods ecGoods = goodsService.selectOne(wrapper1);
        ecCartDto.setGoodsSn(ecGoods.getGoodsSn());
        ecCartDto.setGoodsName(ecGoods.getName());

        BigDecimal number = new BigDecimal(ecCartDto.getNumber());
        ecCartDto.setPrice(ecGoods.getRetailPrice().multiply(number));
        ecCartDto.setPicUrl(ecGoods.getPicUrl());
        ecCartDto.setDeleted(0);
        ecCartDto.setAddTime(new Date());
        EcCart cart = new EcCart();
        BeanUtils.copyProperties(ecCartDto,cart);
        cartService.insert(cart);
        return ResultEntity.ok("恭喜您！该商品成功加入购物车！！");
    }

}
